package algorithm.leetcode.medium;

/*
 给定两个整数 n 和 k，返回 1 ... n 中所有可能的 k 个数的组合。
 */

import java.util.ArrayList;
import java.util.List;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020-11-15
 */
public class Question77 {

    public static void main(String[] args) {
        Question77 question = new Question77();
        List<List<Integer>> lists = question.combine(4, 2);
        System.out.println(lists);
    }


    List<List<Integer>> res = new ArrayList<>(10);
    List<Integer> path = new ArrayList<>(10);

    public List<List<Integer>> combine(int n, int k) {
        backTracking(n, k, 1);
        return res;
    }

    private void backTracking(int n, int k, int index) {
        if (path.size() == k) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = index; i <= n; i++) {
            path.add(i);
            backTracking(n, k, i + 1);
            path.remove(path.size() - 1);
        }
    }

}
